const tokens = {
	admin: "admin-token",
	guest: "guest-token",
	editor: "editor-token",
}

const users = {
	"admin-token": {
		id: "admin",
		role: "admin",
		name: "难凉热血",
		avatar: "https://s1.ax1x.com/2020/04/28/J5hUaT.jpg",
		description: "拥有系统内所有菜单和路由权限",
	},
	"editor-token": {
		id: "editor",
		role: "editor",
		name: "编辑员",
		avatar: "https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png",
		description: "可以看到除户管理页面之外的所有页面",
	},
	"guest-token": {
		id: "guest",
		role: "guest",
		name: "游客",
		avatar: "https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png",
		description: "仅能看到Dashboard、开发文档、权限测试和关于作者四个页面",
	},
}

export default [
	// 登录
	{
		url: '/api/login',
		method: 'post',
		response: (req) => {
			const { username } = req.body;
			const token = tokens[username];
			if (!token) {
				return {
					status: 1,
					message: "用户名或密码错误",
				};
			}
			return {
				status: 0,
				token,
			};
		}
	},

	// 用户信息
	{
		url: '/api/userInfo',
		method: 'post',
		response: (req) => {
			const token = req.headers.token;
			const userInfo = users[token];
			if (!userInfo) {
				return {
					status: 1,
					message: "获取用户信息失败",
				};
			}
			return {
				status: 0,
				userInfo,
			};
		}
	},

	// 获取用户
	{
		url: '/api/users',
		response: () => {
			return {
				status: 0,
				users: Object.values(users),
			}
		}
	},

	{
		url: '/api/delete/user',
		response(req) {
			const { id } = req.body
			const token = tokens[id];
			if (token) {
				delete tokens[id];
				delete users[token];
			}
			return {
				status: 0,
			};
		}
	},

	{
		url: '/api/edit/user',
		response(req) {
			const data = req.body
			const { id } = data;
			const token = tokens[id];
			if (token) {
				users[token] = { ...users[token], ...data };
			}
			return {
				status: 0,
			};
		}
	},

	{
		url: '/api/valid/user',
		response(req) {
			const userID = req.body;
			const token = tokens[userID];
			if (token) {
				return {
					status: 1,
				};
			} else {
				return {
					status: 0
				};
			}
		}
	},

	{
		url: '/api/add/user',
		response(req) {
			const data = req.body
			const { id } = data;
			tokens[id] = `${id}-token`
			users[`${id}-token`] = {
				...users["guest-token"],
				...data
			}
			return {
				status: 0,
			};
		}
	},

	{
		url: '/api/logout',
		response() {
			return {
				status: 0,
				data: "success",
			};
		}
	}
]